Use case: OPAMP in standalone mode

The use case configures the OPAMP in standalone mode, providing maximum flexibility for custom analog signal processing with external components, and ensures reliable operation through self-calibration.

Description of usage

Description

HAL1

HAL2

System Initialization

Initialize HAL library and system peripherals

  • HAL_Init()

  • Configure system clock via SystemClock_Config()

Initialize HAL library and system peripherals

  • HAL_Init()

  • Configure system clock via SystemClock_Config()

GPIO Configuration for OPAMP Input

Enable GPIOx clock

  • __HAL_RCC_GPIOx_CLK_ENABLE()

  • Configure GPIO pin as Analog mode, No Pull

Enable GPIOx clock

  • HAL_RCC_GPIOx_CLK_ENABLE()

  • Configure GPIOx pin as Analog mode, No Pull

OPAMP Initialization

Set OPAMP parameters individually then Call HAL_OPAMP_Init(&hopamp1)

Set OPAMP parameters via config struct then Call HAL_OPAMP_Init(&hopamp1), HAL_OPAMP_SetConfig(&hopamp1, &hconfig)

OPAMP Self-Calibration

Call HAL_OPAMP_SelfCalibrate(&hopamp1)

Call HAL_OPAMP_Calibrate(&hopamp1,HAL_OPAMP_POWER_MODE_NORMAL)

Start OPAMP

Call HAL_OPAMP_Start(&hopamp1)

Call HAL_OPAMP_Start(&hopamp1)

Sequence diagram

OPAMP in standalone mode

  @startuml
  participant "Main" as Main
  participant "HAL Library" as HAL
  participant "RCC Peripheral" as RCC
  participant "GPIO Peripheral" as GPIO
  participant "OPAMP Peripheral" as OPAMP

  == System Initialization ==
  Main -> HAL: HAL_Init()
  note right: Initialize HAL library\nand system peripherals

  Main -> RCC: SystemClock_Config()
  note right: Configure system clock\nfor proper timing

  == GPIO Configuration for OPAMP Input ==
  Main -> GPIO: Enable GPIOx clock
  note right: __HAL_RCC_GPIOx_CLK_ENABLE()

  Main -> GPIO: Configure GPIOx pin\nMode = Analog, Pull = No Pull
  GPIO --> Main: Pins configured
  note right: Set GPIO pins to analog mode\nDisable digital input/output

  == OPAMP Initialization ==
  Main -> OPAMP: Set Instance = OPAMP1
  Main -> OPAMP: Set Mode = standalone
  Main -> OPAMP: Set NonInvertingInput = IO0
  Main -> OPAMP: Set PowerMode = Normal
  Main -> OPAMP: Set PowerSupplyRange  = HIGH
  Main -> OPAMP: Set PowerMode  = NORMAL
  Main -> OPAMP: Set UserTrimming = User
  Main -> HAL: HAL_OPAMP_Init(&hopamp1)
  HAL --> Main: HAL_OK / HAL_ERROR

  note right: Initialize OPAMP with specified parameters

  == OPAMP Self-Calibration ==
  Main -> HAL: HAL_OPAMP_SelfCalibrate(&hopamp1)
  HAL --> Main: HAL_OK / HAL_ERROR

  note right: Perform self-calibration to get trimming values

  == Start OPAMP ==
  Main -> HAL: HAL_OPAMP_Start(&hopamp1)
  HAL --> Main: HAL_OK / HAL_ERROR

  note right: Enable OPAMP to start amplification

  @enduml

Code snippet

HAL1

HAL2

OPAMP_HandleTypeDef hopamp1;

void SystemClock_Config(void);
void Error_Handler(void);



int main(void)
{
    HAL_Init();
    SystemClock_Config();

    // Configure GPIO pin for OPAMP input (example: PA0)
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // 1. Initialize OPAMP handle
    hopamp1.Instance = OPAMP1;



    // 2. Configure OPAMP in standalone  mode
    hopamp1.Init.Mode = OPAMP_STANDALONE_MODE;
    hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0;
    hopamp1.Init.InvertingInput = OPAMP_INVERTINGINPUT_IO0;
    hopamp1.Init.PowerSupplyRange = OPAMP_POWERSUPPLY_HIGH;
    hopamp1.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
    hopamp1.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;

    if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }





    // 3. Perform self-calibration to get trimming values
    if (HAL_OPAMP_SelfCalibrate(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    // 4. Start OPAMP
    if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    /* Add your processing code here */
}

void SystemClock_Config(void)
{
    // Configure system clock as needed
}

void Error_Handler(void)
{
    while(1);
}
hal_opamp_handle_t hopamp1;
hal_opamp_config_t hconfig;
hal_opamp_config_input_connection_t hconfig_input;

void SystemClock_Config(void);
void Error_Handler(void);

int main(void)
{
    HAL_Init();
    SystemClock_Config();

    // Configure GPIO pin for OPAMP input
    HAL_RCC_GPIOA_CLK_ENABLE();
    hal_gpio_config_t GPIO_Config;
    GPIO_Config.Mode = HAL_GPIO_MODE_ANALOG;
    GPIO_Config.Pull = HAL_GPIO_PULL_NO;
    HAL_GPIO_Init(GPIOA, HAL_GPIO_PIN_7, &GPIO_Config);

    // 1. Initialize OPAMP handle
    if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    // 2. Configure OPAMP in standalone  mode
    hconfig.configuration_mode = HAL_OPAMP_MODE_STANDALONE;
    hconfig.power_supply_range = HAL_OPAMP_POWER_SUPPLY_HIGH;
    hconfig.power_mode = HAL_OPAMP_POWER_MODE_NORMAL ;
    hconfig_input.inverting_input = HAL_OPAMP_INVERTING_INPUT_GPIO_0;
    hconfig_input.non_inverting_input = HAL_OPAMP_NON_INVERTING_INPUT_GPIO ;


    if (HAL_OPAMP_SetConfig(&hopamp1, &hconfig) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_OPAMP_SetConfigInputConnection(&hopamp1, &hconfig_input) != HAL_OK)
    {
        Error_Handler();
    }



    // 3. Perform self-calibration to get trimming values
    if (HAL_OPAMP_Calibrate(&hopamp1 , HAL_OPAMP_POWER_MODE_NORMAL) != HAL_OK)
    {
        Error_Handler();
    }

    // 4. Start OPAMP
    if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
    {
        Error_Handler();
    }

    /* Add your processing code here */
}

void SystemClock_Config(void)
{
    // Configure system clock as needed
}

void Error_Handler(void)
{
    while(1);
}

Key changes

  1. In HAL2, OPAMP configuration is provided using a configuration structure with HAL_OPAMP_SetConfig() and input connections are configured with HAL_OPAMP_SetConfigInputConnection().

  2. The calibration API changes from HAL_OPAMP_SelfCalibrate() (HAL1) to HAL_OPAMP_Calibrate() (HAL2).